package com.mengtian_tech.tju.dataquery.api.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mengtian_tech.tju.dataquery.api.model.dataAIMysql.SysRole;
import com.mengtian_tech.tju.dataquery.api.service.PermissionService;
import com.mengtian_tech.tju.dataquery.api.util.ResponseUtil;
import com.mengtian_tech.tju.dataquery.api.vo.User;

@RestController
@RequestMapping("/api/admin/permission")
public class PermissionController {

	@Autowired
	PermissionService permissionService;
	
	@RequestMapping("roleList")
	public Object roleList() {
		return ResponseUtil.ok( this.permissionService.getRoleList()) ;
	}

	@RequestMapping("allMenuList")
	public Object allMenuList() {
		return ResponseUtil.ok( this.permissionService.getAllMenuList()) ;
	}
	
	@PostMapping("saveRole")
	public Object saveRole(@RequestBody SysRole role) {
		
		//首先保存角色
		this.permissionService.saveRole(role);
		
		return ResponseUtil.ok();
		
	}
	
	@RequestMapping("getRoleMenus")
	public Object getRoleMenus(String roleId) {
		return ResponseUtil.ok( this.permissionService.getRoleMenus(roleId)) ;
	}
	
	@PostMapping("deleteRole")
	public Object deleteRole(String roleId) {
		 this.permissionService.deleteRole(roleId);
		return ResponseUtil.ok() ;
	}
	
	
	@RequestMapping("userList")
	public Object userList(Integer page,String id,String name) {
		int pageSize = 20;
		List<User> list = this.permissionService.getGrantUserList(page, pageSize,id,name);
		
		long total = this.permissionService.getGrantUserListCount();
		return ResponseUtil.okList(list,(page-1)*pageSize,total,pageSize);
				
	}
	
	@PostMapping("saveUserRole")
	public Object saveUserRole(@RequestBody User user) {
		
		//删除用户角色
		this.permissionService.deleteUserRoles(user.getId());
		
		//保存
		this.permissionService.saveUserRoles(user.getId(),user.getUserRoles());
		
		return ResponseUtil.ok();
		
	}
	
	@PostMapping("deleteUser")
	public Object deleteUser(String userId) {
		this.permissionService.deleteUserRoles(userId);
		return ResponseUtil.ok() ;
	}
	
	@RequestMapping("getUserRoles")
	public Object getUserRoles(String userId) {
		return ResponseUtil.ok( this.permissionService.getUserRoles(userId)) ;
	}
}
